package com.example.readcontactdemo;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends PermissionActivity {
    private ListView listView;
    private List<Map<String, Object>> list;
    private boolean isGranted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listView);
        list = new ArrayList<>();
        readContacts(); // 调用读取通讯录的方法
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"img", "name", "phone"},
                new int[]{R.id.imageView, R.id.name, R.id.phone}); // 自定义布局
        listView.setAdapter(simpleAdapter);
        simpleAdapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.DIAL");
                intent.setData(Uri.parse("tel:" + list.get(position).get("phone")));
                startActivity(intent);

            }
        });
    }

    // 读取通讯录的方法
    private void readContacts() {
        // 请求授权
        requessPermission();
        if (isGranted) {
            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Map map = new HashMap();
                    map.put("img", R.drawable.tou);
                    map.put("name", name);
                    map.put("phone", phone);
                    list.add(map);
                }
            }
        }
    }

    // 请求授权方法
    private void requessPermission() {
        String[] permission = new String[]{Manifest.permission.READ_CONTACTS}; //读取通讯录的授权
        requestRunTimePermission(permission, new PermissionListener() {
            @Override
            public void onGranted() {
                isGranted = true;
            }

            @Override
            public void onGranted(List<String> grantedPermission) {

            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                isGranted = false;
            }
        });
    }

}